﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DSS_XNA.Engine.Error;
using DSS_XNA.Engine.Script.Variables;

namespace DSS_XNA.Engine.Script.Commands
{
    public class ScriptCommand_Declare
    {
        #region Static Methods

        public static void Action(Dictionary<string, string> args, ScriptManager manager, Game1 engine)
        {
            if (!args.ContainsKey("name"))
                throw new DSSException("No Name parameter for Declare");
            if (!args.ContainsKey("type"))
                throw new DSSException("No Type parameter for Declare");

            string name = manager.GetParameterValue(args["name"]).ToString().ToLower().Trim();
            string type = manager.GetParameterValue(args["type"]).ToString().ToLower().Trim();

            IVariable var = null;
            if (type == "object")
            {
                var = new Variables.VariableObjects.Object();
            }
            else if (type == "int")
            {
                var = new Variables.VariableObjects.Integer();
            }
            else if (type == "float")
            {
                var = new Variables.VariableObjects.Float();
            }
            else if (type == "bool")
            {
                var = new Variables.VariableObjects.Boolean();
            }
            else if (type == "string")
            {
                var = new Variables.VariableObjects.String();
            }
            else
            {
                throw new DSSException(String.Format("'{0}' is an unknown variable type.",
                    type));
            }

            var.Name = name;

            if (args.ContainsKey("value"))
            {
                object value = manager.GetParameterValue(args["value"]);
                var.Value = value;
            }

            VariableScope scope = VariableScope.Local;

            if (args.ContainsKey("scope"))
            {
                string s = manager.GetParameterValue(args["scope"]).ToString().ToLower().Trim();

                if (s == "global")
                {
                    scope = VariableScope.Global;
                }
                else if (s == "local")
                {
                    scope = VariableScope.Local;
                }
                else
                {
                    throw new DSSException(String.Format("'{0}' is an unknown variable scope",
                        s));
                }
            }

            var.Scope = scope;

            if (scope == VariableScope.Global)
            {
                manager.AddGlobalVariable(var);
            }
            else if (scope == VariableScope.Local)
            {
                manager.AddLocalVariable(var);
            }
            else
            {
                throw new DSSException(String.Format("'{0}' is an unknown variable scope",
                    scope));
            }
        }

        #endregion
    }
}
